package com.junjie.checkin.controller;

import com.junjie.checkin.entity.Enum.GIftStatusEnum;
import com.junjie.checkin.entity.Enum.StudentGiftStatusEnum;
import com.junjie.checkin.request.gift.GiftOrderPageRequest;
import com.junjie.checkin.request.gift.GiftPageRequest;
import com.junjie.checkin.request.gift.GiftRequest;
import com.junjie.checkin.response.PageResult;
import com.junjie.checkin.response.admin.AdminGiftOrderResponse;
import com.junjie.checkin.response.admin.AdminGiftResponse;
import com.junjie.checkin.response.Result;
import com.junjie.checkin.response.ResultStatus;
import com.junjie.checkin.response.auth.AdminGiftDetailsResponse;
import com.junjie.checkin.service.GiftService;
import com.junjie.checkin.service.StudentGiftService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 洪浚杰
 * @description
 * @date 2025/6/16 09:16
 */
@RestController
@RequestMapping("/admin/gifts")
public class AdminGiftController {
    @Autowired
    private GiftService service;
    @Autowired
    private StudentGiftService studentGiftService;

    /**
     * 添加礼品
     * @param giftRequest 礼品信息
     * @param request HTTP请求, 用于获取用户信息
     * @return 统一响应, 包含添加的礼品信息
     */
    @PostMapping("")
    public Result<AdminGiftResponse> insertGift(@Validated @RequestBody GiftRequest giftRequest, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        AdminGiftResponse adminGiftResponse = service.insertGift(giftRequest, token);
        return Result.getResult(ResultStatus.OK,adminGiftResponse);
    }

    /**
     * 修改礼品
      * @param id 礼品ID
     * @param giftRequest 礼品信息
     * @param request HTTP请求, 用于获取用户信息
     * @return 统一响应,无返回数据
     */
    @PutMapping("/{id}")
    public Result updateGift(@PathVariable Integer id,@Validated @RequestBody GiftRequest giftRequest,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        service.updateGift(giftRequest, token,id);
        return Result.getResult(ResultStatus.OK) ;
    }
    // 下架礼品
    @DeleteMapping("/{id}")
    public Result deleteGift(@PathVariable Integer id,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        service.deleteGift(id,token);
        return Result.getResult(ResultStatus.OK);

    }

    /**
     * 获取礼品列表
     * @param pageRequest 获取礼品列表的参数
     * @return 统一响应, 包含礼品列表
     */
    @GetMapping("")
    public Result<PageResult<AdminGiftResponse>> getGifts(GiftPageRequest pageRequest){
        pageRequest.initDefaults();
        PageResult<AdminGiftResponse> pageResult = service.getGifts(pageRequest);
        return Result.getResult(ResultStatus.OK,pageResult);
    }

    /**
     * 获取礼品详情
     * @param id 查询的礼品信息
     * @return 统一响应, 礼品详情
     */
    @GetMapping("/{id}")
    public Result<AdminGiftDetailsResponse> getGiftById(@PathVariable Integer id){
        AdminGiftDetailsResponse giftInfo = service.getGiftById(id);
        return Result.getResult(ResultStatus.OK,giftInfo);
    }


    /**
     * 获取指定礼品订单
     * @param id 礼品ID
     * @param pageRequest 获取礼品订单的参数
     * @return  统一响应, 礼品订单列表
     */
    @GetMapping("/{id}/order")
    public Result<PageResult<AdminGiftOrderResponse>> getGiftOrder(@PathVariable Integer id, GiftOrderPageRequest pageRequest){
        PageResult<AdminGiftOrderResponse> giftOrder = studentGiftService.getGiftOrderByGiftId(id, pageRequest);
        return Result.getResult(ResultStatus.OK,giftOrder);
    }

    /**
     * 修改礼品订单状态
     * @param id 订单 ID
     * @param status 状态
     * @param request HTTP 请求, 用于获取用户信息
     * @return 统一响应, 无返回数据
     */
    @PostMapping("/order/{id}")
    public Result updateOrderStatus(@PathVariable Integer id, @Validated @NonNull String status, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        studentGiftService.updateOrderStatus(id, StudentGiftStatusEnum.fromValue(status),token);
        return Result.getResult(ResultStatus.OK);
    }
}
